<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\EcpmFilter;
use App\Admin\Controllers\Helpers\LinkHelper;
use App\Constant\AdrewardEnum;
use App\Models\TaskDetails;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class TaskDetailController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TaskDetails(), function (Grid $grid) {
            $grid->model()->with(['user']);
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('task_id')->sortable();
            $grid->column('user_id');
            $grid->column('media_price', '平台收益');
            $grid->column('media_price', '平台收益');
            $grid->column('reward', '用户收益');

            $grid->column('source')->using([
                'xzb_task' => '小转帮',
                'xianwan_task' => '闲玩',
                'qianliang_task' => 'qianliang',
                'duomi' => 'duomi',
                'shangbanwan_task' => 'shangbanwan',
                'qie_task' => 'qie',
            ]);
            $grid->column('amount');
            $grid->column('exchange_rate', '比例');
            $grid->column('created_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('adreward_id');
                $filter->equal('user_id', '用户ID');

                $filter->in('adtype_id', '广告类型')->checkbox(
                    DB::table('adtypes')->pluck('name', 'id')->toArray()
                );
                $filter->in('adplatform_name', '平台名称')->checkbox(
                    DB::table('adreward_details')->where('adplatform_name', '<>', '')->groupBy('adplatform_name')->pluck('adplatform_name', 'adplatform_name')->toArray()
                );

                $filter->whereBetween('created_at', function ($q) {
                    $start = request('created_at.start') ? (request('created_at.start')) : null;
                    $end = request('created_at.end') ? (request('created_at.end')) : null;
                    if ($start) $q->where('created_at', '>=', $start);
                    if ($end) $q->where('created_at', '<=', $end);
                })->datetime();
            });

            $grid->disableCreateButton();
            $grid->disableBatchDelete();
            $grid->disableActions();
            $grid->fixColumns(1, -2);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TaskDetails(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('adreward_id');
            $show->field('amount');
            $show->field('adtype_id');
            $show->field('adtype_name');
            $show->field('admodel_id');
            $show->field('admodel_value');
            $show->field('reward_rate');
            $show->field('exchange_rate');
            $show->field('real_money');
            $show->field('user_money');
            $show->field('displayed_at');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    public function userTaskDetails(Content $content)
    {
        $grid = $this->userAdrewardGrid(request('user_id'));

        return view('simple', [
            'content' => $grid->render(),
        ]);
    }

    protected function userAdrewardGrid($userId)
    {
        return Grid::make(new TaskDetails(), function (Grid $grid) use ($userId) {
            $grid->model()->with(['user'])->where('user_id', $userId);
            $grid->model()->orderBy('id', 'desc');

            $grid->column('id')->sortable();
            $grid->column('adreward_id')->sortable();
            $grid->column('user_id');
            $grid->column('appcode', 'APP版本')->label();
            $grid->column('amount')->sortable();
            $grid->column('reward_rate')->sortable();
            $grid->column('exchange_rate')->sortable();
            $grid->column('ecpm', 'ECPM')->sortable();
            $grid->column('adplatform_name', '平台名称');
            $grid->column('adtype_name');
            $grid->column('admodel_value');
            $grid->column('user_ip', '用户IP');
            $grid->column('collect_status', '明细状态')
                ->using(AdrewardEnum::STATUS_EXPLAINS)
                ->label(AdrewardEnum::STATUS_COLORS)
                ->sortable();
            $grid->column('displayed_at')->sortable();
            $grid->column('created_at')->sortable();

            $grid->disableCreateButton();
            $grid->disableBatchDelete();
            $grid->disableActions();

        });
    }
}
